package com.yunhe.authority.controller.authority;

import com.yunhe.authority.domain.authority.BackUser;
import com.yunhe.authority.service.authority.BackUserService;
import com.yunhe.authority.service.authority.TokenService;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.UsernamePasswordException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.util.DateTimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author zhengbo
 * @date 2021/10/22 9:00
 */
@Api(value = "backLogin",description = "后台登录", tags = {"后台登录"})
@RestController
@RequestMapping("/backLogin")
public class BackLoginController {

    @Autowired
    private BackUserService backUserService;

    @Autowired
    private TokenService tokenService;

    /**
     * 根据手机号和密码获得用户（执行登录）
     * @param phone 键
     * @param password 值
     */
    @ApiOperation("根据手机号和密码获得用户（执行登录）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "systemId", value = "系统标识", dataType = "string", paramType = "query")
    })
    @RequestMapping(value="/loginByPhone", method = RequestMethod.POST)
    public ResponseEntity<ResultObject> login(@RequestParam("phone") Optional<String> phone,
                                              @RequestParam("password") Optional<String> password,
                                              @RequestParam("systemId") Optional<String> systemId)
            throws ArgumentErrorException, UsernamePasswordException {
        if (!phone.isPresent()) {
            throw new UsernamePasswordException("手机号不能为空");
        } else if (!password.isPresent()) {
            throw new UsernamePasswordException("密码不能为空");
        }else if(!systemId.isPresent()){
            throw new UsernamePasswordException("系统标识不能为空");
        }
        BackUser backUser = backUserService.findByPhoneAndPassword(phone.get(), password.get());
        String token = tokenService.getBackToken(backUser, systemId.get());
        Map<String, Object> result = new HashMap<>(4);
        result.put("token", token);
        result.put("user", backUser);
        //记录最后登录时间
        backUser.setLastLoginTime(DateTimeUtils.format(DateTimeUtils.PATTERN.DATETIME.value(),new Date()));
        backUserService.update(backUser);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }
}
